package org.dolphin.service.misc.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.dolphin.base.bean.LocaleVO;
import org.dolphin.dao.LocaleDao;
import org.dolphin.entity.misc.UserLocale;
import org.dolphin.service.misc.LocaleService;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "localeService")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class LocaleServiceImpl implements LocaleService {
  @Autowired(required = true)
  private LocaleDao localeDao;

  public LocaleDao getLocaleDao() {
    return localeDao;
  }

  @Override
  public List<LocaleVO> getLocales() {
    List<Order> orders = new ArrayList<Order>(1);
    orders.add(Order.asc("description"));
    List<UserLocale> list = localeDao.getUserLocales(orders);
    List<LocaleVO> results = Collections.emptyList();
    if (list != null && !list.isEmpty()) {
      results = new ArrayList<LocaleVO>(list.size());
      for (UserLocale locale : list) {
        LocaleVO vo = new LocaleVO();
        vo.setId(locale.getId());
        vo.setDescription(locale.getDescription());
        results.add(vo);
      }
    }
    return results;
  }

  @Override
  public UserLocale loadLocaleById(Long localeId) {
    return localeDao.loadUserLocale(localeId);
  }

  public void setLocaleDao(LocaleDao localeDao) {
    this.localeDao = localeDao;
  }
}
